Imports DiceShenanigensUserControls

Public Class frmGrid
   Implements GridFormInterface

   Private place() As gridBox
   Private gridx As Integer = 15
   Private gridy As Integer = 15
   Private placeWidth As Integer = 35
   Private placeHeight As Integer = 35
   Private numOfBlackSquares As Integer = 50
   Private player1Color As Color
   Private player2Color As Color
   Private player1squares As Integer
   Private player2squares As Integer
   Private reinforcementPhase As Boolean = False
   Private numReinforcements As Integer = 0
   Private timerticker As Integer = 0
   Private numOfPlayers As Integer

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="numPlayers"></param>
   ''' <param name="size"></param>
   ''' <param name="player1Color"></param>
   ''' <param name="player2Color"></param>
   ''' <remarks></remarks>
   Public Sub New(ByVal numPlayers As Integer, ByVal size As GridSize, ByVal player1Color As Color, ByVal player2Color As Color)

      InitializeComponent()
      playerTurn = PlayerNumber.Player1

      Me.player1Color = player1Color
      Me.player2Color = player2Color
      txt2Squares.BackColor = player2Color
      txt1Squares.BackColor = player1Color
      player1border.BackColor = Color.FromArgb(247, 148, 29)
      player2border.BackColor = Color.FromArgb(105, 105, 105)

      numOfPlayers = numPlayers

      gridx = size
      gridy = size

      player1squares = ((gridx * gridy) / 2)
      player2squares = ((gridx * gridy) / 2)
      player1squares += 1
      txt1Squares.Text = player1squares
      txt2Squares.Text = player2squares
   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <remarks></remarks>
   Private Sub draw() Implements GridFormInterface.draw
      Dim numofplayer1 As Integer = player1squares
      Dim numofplayer2 As Integer = player2squares

      Dim c As Integer = 0

      For x As Integer = 1 To gridx
         For y As Integer = 1 To gridy

            ReDim Preserve place(c)

            Dim rand As Integer = randGenerator.Next(0, 2)

            If (rand < 1) And (numofplayer1 > 0) Then
               numofplayer1 -= 1
               place(c) = New gridBox(5, player1Color, 1, Me, gridMode.Playing)
            ElseIf (rand >= 1) And (numofplayer2 > 0) Then
               numofplayer2 -= 1
               place(c) = New gridBox(5, player2Color, 2, Me, gridMode.Playing)
            ElseIf (numofplayer2 > 0) Then
               numofplayer2 -= 1
               place(c) = New gridBox(5, player1Color, 1, Me, gridMode.Playing)
            ElseIf (numofplayer1 > 0) Then
               numofplayer1 -= 1
               place(c) = New gridBox(5, player2Color, 2, Me, gridMode.Playing)
            End If


            'If owners(c) = 1 Then
            '   place(c) = New gridBox(points(c), player1Color, owners(c), Me, gridMode.Playing)
            'ElseIf owners(c) = 2 Then
            '   place(c) = New gridBox(points(c), player2Color, owners(c), Me, gridMode.Playing)
            'End If


            place(c).Name = c
            place(c).Size = New Size(placeWidth, placeHeight)
            place(c).Location = New Point(placeWidth * (x - 1), placeHeight * (y - 1))
            gridPanel.Controls.Add(place(c))
            c += 1

         Next y
      Next x
      c -= 1
      gridPanel.Width = place(c).Right + 5
      gridPanel.Height = place(c).Bottom + 5
      Me.Width = place(c).Right + place(c).Width + 200
      Me.Height = place(c).Bottom + (2 * place(c).Height)

      Me.Top = (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 2) - (Me.Height / 2)
      Me.Left = (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2) - (Me.Width / 2)
   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="attackerNum"></param>
   ''' <param name="defenderNum"></param>
   ''' <remarks></remarks>
   Public Sub battle(ByVal attackerNum As Integer, ByVal defenderNum As Integer) Implements GridFormInterface.battle

      Dim attackRoll As Integer
      Dim defendRoll As Integer
      Dim attacker As gridBox = CType(gridPanel.Controls.Item(attackerNum), gridBox)
      Dim defender As gridBox = CType(gridPanel.Controls.Item(defenderNum), gridBox)
      Dim i As Integer = 0

      For i = 0 To attacker.numDice
         attackRoll += randGenerator.Next(1, 6)
         txtAttack.Text = attackRoll
      Next

      For i = 0 To defender.numDice
         defendRoll += randGenerator.Next(1, 6)
         txtDefend.Text = defendRoll
      Next

      attacker.colorUp()

      If (defendRoll < attackRoll) Then

         defender.numDice = attacker.numDice - 1
         defender.playColor = attacker.playColor

         If defender.player = PlayerNumber.Player1 Then

            defender.player = PlayerNumber.Player2
            player1squares -= 1
            player2squares += 1

         Else

            defender.player = PlayerNumber.Player1
            player1squares += 1
            player2squares -= 1

         End If

         txt1Squares.Text = player1squares
         txt2Squares.Text = player2squares

      End If

      attacker.numDice() = 1
      checkBorders()

   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub frmGrid_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      gridPanel.Left = 15
      gridPanel.Top = 25

      draw()
      checkBorders()

   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <remarks></remarks>
   Private Sub checkBorders()

      Dim i As Integer
      Dim one As Integer = 0
      Dim two As Integer = 0

      For i = 0 To (gridx * gridx) - 1

         If (CType(gridPanel.Controls.Item(i), gridBox).player = PlayerNumber.Player1) Then
            one += 1
         ElseIf (CType(gridPanel.Controls.Item(i), gridBox).player = PlayerNumber.Player2) Then
            two += 1
         End If

         checkUp(i)
         checkleft(i)

      Next

      If one = gridx * gridy Then

         winner(PlayerNumber.Player1)

      ElseIf two = gridx * gridy Then

         winner(PlayerNumber.Player2)

      End If

   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="squareToCheck"></param>
   ''' <remarks></remarks>
   Private Sub checkUp(ByVal squareToCheck As Integer)

      Dim square As gridBox = CType(gridPanel.Controls.Item(squareToCheck), gridBox)

      If Not (squareToCheck Mod gridx = 0) Then

         If square.player = CType(gridPanel.Controls.Item(squareToCheck - 1), gridBox).player Then

            square.topBorder = False

         Else

            square.topBorder = True

         End If

      End If

   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="squareToCheck"></param>
   ''' <remarks></remarks>
   Private Sub checkleft(ByVal squareToCheck)

      Dim square As gridBox = CType(gridPanel.Controls.Item(squareToCheck), gridBox)

      If Not (squareToCheck >= 0 And squareToCheck < gridy) Then

         If square.player = CType(gridPanel.Controls.Item(squareToCheck - gridy), gridBox).player Then

            square.leftBorder = False

         Else

            square.leftBorder = True

         End If

      End If

   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public ReadOnly Property gridHeight() As Integer Implements GridFormInterface.gridHeight
      Get
         Return gridy
      End Get
   End Property

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Property reinforceStage() As Boolean Implements GridFormInterface.reinforceStage
      Get
         Return reinforcementPhase
      End Get
      Set(ByVal value As Boolean)
         reinforcementPhase = value
      End Set
   End Property

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="num"></param>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Property blackSquares(ByVal num) As Integer
      Get
         Return numOfBlackSquares
      End Get
      Set(ByVal value As Integer)
         numOfBlackSquares = value
      End Set
   End Property

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Property numReinforce() As Integer Implements GridFormInterface.numReinforce
      Get
         Return numReinforcements
      End Get
      Set(ByVal value As Integer)
         numReinforcements = value
         txtReinforcements.Text() = value
      End Set
   End Property

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub btnTurn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTurn.Click

      If reinforcementPhase Then

         reinforcementPhase = False
         btnTurn.Text = "Get Reinforcements"
         numReinforce = 0

         If playerTurn = PlayerNumber.Player1 Then

            playerTurn = PlayerNumber.Player2
            player2border.BackColor = Color.FromArgb(247, 148, 29)
            player1border.BackColor = Color.FromArgb(105, 105, 105)

         Else

            playerTurn = PlayerNumber.Player1
            player1border.BackColor = Color.FromArgb(247, 148, 29)
            player2border.BackColor = Color.FromArgb(105, 105, 105)

         End If

      Else

         reinforcementPhase = True
         btnTurn.Text = "End Turn"

         If playerTurn = PlayerNumber.Player1 Then
            numReinforce = player1squares
         Else
            numReinforce = player2squares
         End If

      End If

   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
      Application.Exit()
   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="winningPlayer"></param>
   ''' <remarks></remarks>
   Private Sub winner(ByVal winningPlayer As PlayerNumber)
      MsgBox("Player " & winningPlayer & " is the winner!")
   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub frmGrid_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
      Application.Exit()
   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub SaveGameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveGameToolStripMenuItem.Click

      Dim i As Integer
      Dim nums() As Integer = {0}

      For i = 0 To (gridx * gridx) - 1
         nums(i) = CType(gridPanel.Controls.Item(i), gridBox).numDice
      Next

   End Sub

End Class
